package com.spark.lola.controller;

import com.spark.lola.support.api.service.UploadService;
import com.spark.lola.support.common.util.ResponseBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: DengYeJun
 * @Date: 2019/4/16 0016 9:47
 */
@Api(value = "/api/huarong/upload/", tags = "上传相关")
@RestController
@RequestMapping("/api/huarong/upload")
public class UploadController {
    @Value("${lola.upload.accessKey}")
    private String uploadAccessKey;
    @Autowired
    private UploadService uploadService;

    @ApiOperation(value = "上传文件")
    @RequestMapping(value = "/img")
    public ResponseBean img(@RequestParam("file") MultipartFile file
            , @RequestParam(value = "path", defaultValue = "deploy") String path) throws IOException {
        String nonce = "adc123";
        long timestamp = System.currentTimeMillis();
        String raw = "" + uploadAccessKey + nonce + timestamp;
        String sign = DigestUtils.md5Hex(raw).toLowerCase();

        Map<String,Object> params =new HashMap<>();
        params.put("nonce",nonce);
        params.put("sign",sign);
        params.put("timestamp",timestamp);
        params.put("path",path);
        return uploadService.upload(file,params);
    }

}
